import { intoRecurse, deepCopy } from '@u/common.js';
import asyncRouters from './asyncRouters';
import moduleRouters from './moduleRouters';

const Layout = () => import(/* webpackChunkName: "layout" */ '@c/Layout'),
  allRouters = [];

asyncRouters.map(item => {
  allRouters.push(...item.children);
});
moduleRouters.map(item => {
  allRouters.push(item);
});

export default function(userPermission, callback) {
  const asyncRouters = [],
    affixRouters = [],
    alreadyAdd = [],
    modules = [],
    menus = [],
    functions = [],
    buttons = {};
  userPermission.map(m => {
    if (m.meta && m.meta.type == 1) {
      const moduleRoute = {},
        mComponent = allRouters.find(r => r.name == m.name);
      moduleRoute.name = m.name;
      moduleRoute.path = '/' + m.name.substring(0, 1).toLowerCase() + m.name.substring(1);
      moduleRoute.meta = mComponent ? { ...mComponent.meta, ...m.meta } : m.meta;
      moduleRoute.component = mComponent ? mComponent.component : Layout;
      moduleRoute.children = [];
      m.isModule = !!mComponent;
      if (m.redirect) {
        moduleRoute.redirect = m.redirect;
      }
      asyncRouters.push(moduleRoute);
    }
  });
  intoRecurse(userPermission, (item, options) => {
    if (item.meta && item.meta.type == 4) { // 拿到所有功能类型
      functions.push(item.name);
    }
    if (item.meta && item.meta.type == 5) { // 拿到所有按钮类型
      callback && callback(buttons, item);
    }

    if (item.level == 1) { // 将模块名传入子级
      modules.push(item.name);
      options.module = item;
    } else { // 第一层为模块类型，所以从第二层开始添加路由
      if (item.children && item.children.length) { // 获取所有上级
        item.children.map(child => {
          if (item.parents) {
            child.parents = deepCopy(item.parents);
          } else {
            child.parents = [];
          }
          child.parents.push({
            name: item.name,
            title: item.meta && item.meta.title,
            type: item.meta && item.meta.type,
            redirect: item.redirect
          });
        });
      }

      if (item.meta && item.meta.type == 3) { // 拿到所有菜单类型
        menus.push(item.name);
      }

      if (!(options.module.name == 'Home' && item.meta.type == 4)) { // 排除首页里的功能
        const router = allRouters.find(r => r.name == item.name);
        if (router) {
          const mRouter = asyncRouters.find(first => first.name == options.module.name);
          if (mRouter && !alreadyAdd.includes(item.name)) {
            router.meta = item.meta;
            router.meta.module = options.module;
            router.meta.parents = item.parents || [];
            mRouter.children.push(router);
            if (router.affix) {
              affixRouters.push({
                name: router.name,
                params: router.params,
                title: router.meta && router.meta.title,
                moduleName: router.meta.module && router.meta.module.name
              });
            }
          }
          if (!alreadyAdd.includes(item.name)) { // 已经添加过的路由
            alreadyAdd.push(item.name);
          }
        }
      }
    }
  });
  return {
    asyncRouters,
    affixRouters,
    modules,
    menus,
    functions,
    buttons
  };
}
